package cn.com.lyj6851.modules.shop.controller;

import cn.com.lyj6851.api.ApiResult;
import cn.com.lyj6851.api.MyShopException;
import cn.com.lyj6851.constant.ShopConstants;
import cn.com.lyj6851.enums.ProductEnum;
import cn.com.lyj6851.modules.activity.service.YdyStoreCombinationService;
import cn.com.lyj6851.modules.activity.service.YdyStoreSeckillService;
import cn.com.lyj6851.modules.mp.service.YdyWechatLiveService;
import cn.com.lyj6851.modules.product.service.YdyStoreProductService;
import cn.com.lyj6851.modules.product.vo.YdySystemStoreQueryVo;
import cn.com.lyj6851.modules.shop.param.YdySystemStoreQueryParam;
import cn.com.lyj6851.modules.shop.service.YdySystemGroupDataService;
import cn.com.lyj6851.modules.shop.service.YdySystemStoreService;
import cn.com.lyj6851.modules.shop.vo.IndexVo;
import cn.com.lyj6851.utils.FileUtil;
import cn.com.lyj6851.utils.RedisUtil;
import cn.com.lyj6851.utils.ShopKeyUtils;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.io.resource.ClassPathResource;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * APP首页数据
 * @author Administrator
 */
@Slf4j
@RestController
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Api(value = "首页模块", tags = "商城:首页模块", description = "首页模块")
public class IndexController {

    private final YdySystemGroupDataService systemGroupDataService;
    private final YdyStoreProductService storeProductService;
    private final YdySystemStoreService systemStoreService;
    private final YdyStoreCombinationService storeCombinationService;
    private final YdyStoreSeckillService storeSeckillService;
    private final YdyWechatLiveService wechatLiveService;


    //@Cacheable(cacheNames = ShopConstants.LYJ_SHOP_REDIS_INDEX_KEY)
    @GetMapping("/index")
    @ApiOperation(value = "首页数据",notes = "首页数据")
    public ApiResult<IndexVo> index(){
        IndexVo indexVo = IndexVo.builder()
                .banner(systemGroupDataService.getDatas(ShopConstants.YDY_HOME_BANNER))
                .bastList(storeProductService.getList(1,6, ProductEnum.TYPE_1.getValue()))
                .benefit(storeProductService.getList(1,10,ProductEnum.TYPE_4.getValue()))
                .firstList(storeProductService.getList(1,6,ProductEnum.TYPE_3.getValue()))
                .likeInfo(storeProductService.getList(1,8,ProductEnum.TYPE_2.getValue()))
                .combinationList(storeCombinationService.getList(1,8).getStoreCombinationQueryVos())
                .mapKey(RedisUtil.get(ShopKeyUtils.getTengXunMapKey()))
                .menus(systemGroupDataService.getDatas(ShopConstants.YDY_HOME_MENUS))
                .roll(systemGroupDataService.getDatas(ShopConstants.YDY_HOME_ROLL_NEWS))
                .seckillList(storeSeckillService.getList(1, 4))
                .liveList(wechatLiveService.getList(1,4,0))
                .build();
        return ApiResult.ok(indexVo);
    }

    @GetMapping("/search/keyword")
    @ApiOperation(value = "热门搜索关键字获取",notes = "热门搜索关键字获取")
    public ApiResult<List<String>> search(){
        List<JSONObject> list = systemGroupDataService.getDatas(ShopConstants.LYJ_SHOP_HOT_SEARCH);
        List<String>  stringList = new ArrayList<>();
        for (JSONObject object : list) {
            stringList.add(object.getString("title"));
        }
        return ApiResult.ok(stringList);
    }


    @PostMapping("/image_base64")
    @ApiOperation(value = "获取图片base64",notes = "获取图片base64")
    @Deprecated
    public ApiResult<List<String>> imageBase64(){
        return ApiResult.ok(null);
    }


    @GetMapping("/citys")
    @ApiOperation(value = "获取城市json",notes = "获取城市json")
    public ApiResult<JSONObject> cityJson(){
        String path = "city.json";
        String name = "city.json";
        try {
            File file = FileUtil.inputStreamToFile(new ClassPathResource(path).getStream(), name);
            FileReader fileReader = new FileReader(file,"UTF-8");
            String string = fileReader.readString();
            System.out.println(string);
            JSONObject jsonObject = JSON.parseObject(string);
            return ApiResult.ok(jsonObject);
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new MyShopException("无数据");
        }

    }


    @GetMapping("/store_list")
    @ApiOperation(value = "获取门店列表",notes = "获取门店列表")
    public ApiResult<Map<String,Object>> storeList( YdySystemStoreQueryParam param){
        Map<String,Object> map = new LinkedHashMap<>();
        List<YdySystemStoreQueryVo> lists = systemStoreService.getStoreList(
                param.getLatitude(),
                param.getLongitude(),
                param.getPage(),param.getLimit());
        map.put("list",lists);
        return ApiResult.ok(map);
    }




}
